package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.MedicineDAO;
import cn.edu.scau.cmi.hombio.dao.MedicinePriceDAO;
import cn.edu.scau.cmi.hombio.dao.MedicineSupplierDAO;
import cn.edu.scau.cmi.hombio.dao.SupplierDAO;

import cn.edu.scau.cmi.hombio.domain.Medicine;
import cn.edu.scau.cmi.hombio.domain.MedicinePrice;
import cn.edu.scau.cmi.hombio.domain.MedicineSupplier;
import cn.edu.scau.cmi.hombio.domain.Supplier;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for MedicineSupplier entities
 * 
 */

@Service("MedicineSupplierService")

@Transactional
public class MedicineSupplierServiceImpl implements MedicineSupplierService {

	/**
	 * DAO injected by Spring that manages Medicine entities
	 * 
	 */
	@Autowired
	private MedicineDAO medicineDAO;

	/**
	 * DAO injected by Spring that manages MedicinePrice entities
	 * 
	 */
	@Autowired
	private MedicinePriceDAO medicinePriceDAO;

	/**
	 * DAO injected by Spring that manages MedicineSupplier entities
	 * 
	 */
	@Autowired
	private MedicineSupplierDAO medicineSupplierDAO;

	/**
	 * DAO injected by Spring that manages Supplier entities
	 * 
	 */
	@Autowired
	private SupplierDAO supplierDAO;

	/**
	 * Instantiates a new MedicineSupplierServiceImpl.
	 *
	 */
	public MedicineSupplierServiceImpl() {
	}

	/**
	 * Save an existing MedicineSupplier entity
	 * 
	 */
	@Transactional
	public void saveMedicineSupplier(MedicineSupplier medicinesupplier) {
		MedicineSupplier existingMedicineSupplier = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier.getId());

		if (existingMedicineSupplier != null) {
			if (existingMedicineSupplier != medicinesupplier) {
				existingMedicineSupplier.setId(medicinesupplier.getId());
			}
			medicinesupplier = medicineSupplierDAO.store(existingMedicineSupplier);
		} else {
			medicinesupplier = medicineSupplierDAO.store(medicinesupplier);
		}
		medicineSupplierDAO.flush();
	}

	/**
	 * Return all MedicineSupplier entity
	 * 
	 */
	@Transactional
	public List<MedicineSupplier> findAllMedicineSuppliers(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<MedicineSupplier>(medicineSupplierDAO.findAllMedicineSuppliers(startResult, maxRows));
	}

	/**
	 * Delete an existing Medicine entity
	 * 
	 */
	@Transactional
	public MedicineSupplier deleteMedicineSupplierMedicine(Integer medicinesupplier_id, Integer related_medicine_id) {
		MedicineSupplier medicinesupplier = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier_id, -1, -1);
		Medicine related_medicine = medicineDAO.findMedicineByPrimaryKey(related_medicine_id, -1, -1);

		medicinesupplier.setMedicine(null);
		related_medicine.getMedicineSuppliers().remove(medicinesupplier);
		medicinesupplier = medicineSupplierDAO.store(medicinesupplier);
		medicineSupplierDAO.flush();

		related_medicine = medicineDAO.store(related_medicine);
		medicineDAO.flush();

		medicineDAO.remove(related_medicine);
		medicineDAO.flush();

		return medicinesupplier;
	}

	/**
	 * Delete an existing Supplier entity
	 * 
	 */
	@Transactional
	public MedicineSupplier deleteMedicineSupplierSupplier(Integer medicinesupplier_id, Integer related_supplier_id) {
		MedicineSupplier medicinesupplier = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier_id, -1, -1);
		Supplier related_supplier = supplierDAO.findSupplierByPrimaryKey(related_supplier_id, -1, -1);

		medicinesupplier.setSupplier(null);
		related_supplier.getMedicineSuppliers().remove(medicinesupplier);
		medicinesupplier = medicineSupplierDAO.store(medicinesupplier);
		medicineSupplierDAO.flush();

		related_supplier = supplierDAO.store(related_supplier);
		supplierDAO.flush();

		supplierDAO.remove(related_supplier);
		supplierDAO.flush();

		return medicinesupplier;
	}

	/**
	 * Delete an existing MedicinePrice entity
	 * 
	 */
	@Transactional
	public MedicineSupplier deleteMedicineSupplierMedicinePrices(Integer medicinesupplier_id, Integer related_medicineprices_id) {
		MedicinePrice related_medicineprices = medicinePriceDAO.findMedicinePriceByPrimaryKey(related_medicineprices_id, -1, -1);

		MedicineSupplier medicinesupplier = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(medicinesupplier_id, -1, -1);

		related_medicineprices.setMedicineSupplier(null);
		medicinesupplier.getMedicinePrices().remove(related_medicineprices);

		medicinePriceDAO.remove(related_medicineprices);
		medicinePriceDAO.flush();

		return medicinesupplier;
	}

	/**
	 */
	@Transactional
	public MedicineSupplier findMedicineSupplierByPrimaryKey(Integer id) {
		return medicineSupplierDAO.findMedicineSupplierByPrimaryKey(id);
	}

	/**
	 * Save an existing MedicinePrice entity
	 * 
	 */
	@Transactional
	public MedicineSupplier saveMedicineSupplierMedicinePrices(Integer id, MedicinePrice related_medicineprices) {
		MedicineSupplier medicinesupplier = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(id, -1, -1);
		MedicinePrice existingmedicinePrices = medicinePriceDAO.findMedicinePriceByPrimaryKey(related_medicineprices.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicinePrices != null) {
			existingmedicinePrices.setId(related_medicineprices.getId());
			existingmedicinePrices.setPrice(related_medicineprices.getPrice());
			related_medicineprices = existingmedicinePrices;
		} else {
			related_medicineprices = medicinePriceDAO.store(related_medicineprices);
			medicinePriceDAO.flush();
		}

		related_medicineprices.setMedicineSupplier(medicinesupplier);
		medicinesupplier.getMedicinePrices().add(related_medicineprices);
		related_medicineprices = medicinePriceDAO.store(related_medicineprices);
		medicinePriceDAO.flush();

		medicinesupplier = medicineSupplierDAO.store(medicinesupplier);
		medicineSupplierDAO.flush();

		return medicinesupplier;
	}

	/**
	 * Save an existing Medicine entity
	 * 
	 */
	@Transactional
	public MedicineSupplier saveMedicineSupplierMedicine(Integer id, Medicine related_medicine) {
		MedicineSupplier medicinesupplier = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(id, -1, -1);
		Medicine existingmedicine = medicineDAO.findMedicineByPrimaryKey(related_medicine.getId());

		// copy into the existing record to preserve existing relationships
		if (existingmedicine != null) {
			existingmedicine.setId(related_medicine.getId());
			existingmedicine.setName(related_medicine.getName());
			existingmedicine.setNumber(related_medicine.getNumber());
			existingmedicine.setCompany(related_medicine.getCompany());
			existingmedicine.setQuantityPerCase(related_medicine.getQuantityPerCase());
			existingmedicine.setQuantityPerBox(related_medicine.getQuantityPerBox());
			existingmedicine.setPhone(related_medicine.getPhone());
			existingmedicine.setMemo(related_medicine.getMemo());
			related_medicine = existingmedicine;
		} else {
			related_medicine = medicineDAO.store(related_medicine);
			medicineDAO.flush();
		}

		medicinesupplier.setMedicine(related_medicine);
		related_medicine.getMedicineSuppliers().add(medicinesupplier);
		medicinesupplier = medicineSupplierDAO.store(medicinesupplier);
		medicineSupplierDAO.flush();

		related_medicine = medicineDAO.store(related_medicine);
		medicineDAO.flush();

		return medicinesupplier;
	}

	/**
	 * Return a count of all MedicineSupplier entity
	 * 
	 */
	@Transactional
	public Integer countMedicineSuppliers() {
		return ((Long) medicineSupplierDAO.createQuerySingleResult("select count(o) from MedicineSupplier o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing MedicineSupplier entity
	 * 
	 */
	@Transactional
	public void deleteMedicineSupplier(MedicineSupplier medicinesupplier) {
		medicineSupplierDAO.remove(medicinesupplier);
		medicineSupplierDAO.flush();
	}

	/**
	 * Load an existing MedicineSupplier entity
	 * 
	 */
	@Transactional
	public Set<MedicineSupplier> loadMedicineSuppliers() {
		return medicineSupplierDAO.findAllMedicineSuppliers();
	}

	/**
	 * Save an existing Supplier entity
	 * 
	 */
	@Transactional
	public MedicineSupplier saveMedicineSupplierSupplier(Integer id, Supplier related_supplier) {
		MedicineSupplier medicinesupplier = medicineSupplierDAO.findMedicineSupplierByPrimaryKey(id, -1, -1);
		Supplier existingsupplier = supplierDAO.findSupplierByPrimaryKey(related_supplier.getId());

		// copy into the existing record to preserve existing relationships
		if (existingsupplier != null) {
			existingsupplier.setId(related_supplier.getId());
			existingsupplier.setName(related_supplier.getName());
			related_supplier = existingsupplier;
		} else {
			related_supplier = supplierDAO.store(related_supplier);
			supplierDAO.flush();
		}

		medicinesupplier.setSupplier(related_supplier);
		related_supplier.getMedicineSuppliers().add(medicinesupplier);
		medicinesupplier = medicineSupplierDAO.store(medicinesupplier);
		medicineSupplierDAO.flush();

		related_supplier = supplierDAO.store(related_supplier);
		supplierDAO.flush();

		return medicinesupplier;
	}
}
